<?php
/**
 * 中奖记录
 */
class Winners extends CS_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->model('lottery_winner_model', 'lottery_winner');
        $this->load->model('region_model', 'region');
        $this->load->model('tourism_delivery_address_model', 'tourism_delivery_address');
        $this->load->model('scenery_base_model','scenery_base');
        $this->load->model('tourism_goods_base_model','tourism_goods_base');
        $this->load->model('user_account_model','user_account');
    }

    /**
     * 中奖记录
     */
    public function grid()
    {
        $data['winner'] = $this->lottery_winner->findByUId($this->uid);
        $this->load->view('home/winners/grid', $data);
    }

    /**
     * 领取奖品
     */
    public function acceptPrize($winnerId)
    {
        $result = $this->lottery_winner->findById($winnerId);
        if ( $result->num_rows() == 0 ) { //中奖者不存在
            $this->redirect('home/winners/grid');
        }
        $winner = $result->row();
        if ( $winner->effective_date <= date('Y-m-d H:i:s') ) { // 奖品已过期
            $this->redirect('home/winners/grid');
        }

        if ( $winner->status != 1 || $winner->prize_type != 1 ) { // 该奖品已领取
            $this->redirect('home/winners/grid');
        }
        $data['winner'] = $winner;
        $data['province'] = $this->region->findByType(1, 1);
        $this->load->view('home/winners/acceptPrize', $data);
    }

    /**
     * 领取奖品提交
     */
    public function acceptPrizePost()
    {
        $postData = $this->input->post();
        $result = $this->lottery_winner->findById($postData['id']);
        if ( $result->num_rows() == 0 ) {//中奖者不存在
            $this->redirect('home/winners/grid');
        }

        if ( $result->row()->effective_date <= date('Y-m-d H:i:s') ) { // 奖品已过期
            $this->redirect('home/winners/grid');
        }

        $result = $this->tourism_delivery_address->findDefaultAddress($this->uid);
        if ( $result->num_rows() > 0 ) {
            $postData['is_default'] = 1;//非默认地址
        } else {
            $postData['is_default'] = 2;//默认地址
        }
        $postData['uid'] = $this->uid;

        $this->db->trans_start();
        $updateId = $this->lottery_winner->updateInfo($postData['id'], array('status' => 2, 'address' => $this->splitJointAddress($postData)));
        $insetId = $this->tourism_delivery_address->insert($postData);
        $this->db->trans_complete();

        if ( $this->db->trans_status() === FALSE ) {
            $this->alertError('数据插入失败');
        } else {
            $this->redirect('home/winners/grid');//奖品领取成功
        }
    }

    /**
     * json格式打包地址
     * @param $data
     * @return mixed|string|void
     */
    private function splitJointAddress($data)
    {
        $address = array();
        $address['receiver_name'] = $data['receiver_name'];
        $address['tel'] = $data['tel'];
        $address['code'] = $data['code'];
        $address['detailed'] = $data['province_name']. $data['city_name']. $data['district_name']. $data['detailed'];
        return json_encode($address );
    }

    /**
     * 领取虚拟奖品
     */
    public function acceptVirtualPrize($winnerId)
    {
        $result = $this->lottery_winner->findById($winnerId);
        if ($result->num_rows() == 0) {
            $this->alertError('中奖者不存在');
        }
        $winner = $result->row();
        if ($winner->effective_date <= date('Y-m-d H:i:s')) { // 奖品已过期
            $this->redirect('home/winners/grid');
        }

        if ($winner->status != 1 || $winner->prize_type != 2) {//该奖品已领取
            $this->redirect('home/winners/grid');
        }
        if ($winner->flag == 1) { //首抽必中 只能是游币
            $this->db->trans_start();
            $this->lottery_winner->updateInfo($winnerId, array('status' => 3));
            $this->user_account->updateUserAccount($this->uid, array('amount_bonus' => 300));//给用户充300游币
            $this->db->trans_complete();
            $this->redirect('home/winners/detail/'. $winnerId);
        } else {
            $data['winner'] = $winner;
            $this->load->view('home/winners/acceptVirtualPrize', $data);
        }
    }

    /**
     * 领取虚拟奖品提交
     */
    public function acceptVirtualPrizePost()
    {
        $postData = $this->input->post();
        $result = $this->lottery_winner->findById($postData['id']);
        if ( $result->num_rows() == 0 ) {//中奖者不存在
            $this->redirect('home/winners/grid');
        }
        if ( $result->row()->effective_date <= date('Y-m-d H:i:s') ) { // 奖品已过期
            $this->redirect('home/winners/grid');
        }
        $updateId = $this->lottery_winner->updateInfo($postData['id'], array('status' => 2, 'address' => json_encode($postData)));
        if ( !$updateId ) {
            $this->alertError('信息更新失败');
        } else {
            $this->redirect('home/winners/grid');//奖品领取成功
        }
    }

    /**
     * 中奖记录详情
     */
    public function detail($winnerId)
    {
        $result = $this->lottery_winner->findById($winnerId);
        if ( $result->num_rows() == 0 ) {
            $this->redirect('home/winners/grid');
        }
        $winner = $result->row();
        if ($winner->status <= 1) {
            $this->redirect('home/winners/grid');
        }
        $data['winner'] = $winner;
        $this->load->view('home/winners/detail', $data);
    }

    /**
     * 获取城市 区域
     */
    public function ajaxRegion()
    {
        $parentId = $this->input->post('parentId');
        $type = $this->input->post('type');

        if ( $parentId <= 0 || $type <= 0 ) {
            return ;
        }

        $result = $this->region->findByType($parentId, $type);
        $status = false;
        $message = '网络错误，请稍后重试';
        $district = '';
        $citiesName = '';
        $districtName = '';
        if ( $result->num_rows() > 0 ) {
            $status = true;
            $message = '';
            $citiesId = 0;
            foreach($result->result() as $key=>$value) {
                if ( 0 == $key ) {
                    $citiesId = 2 == $type ? $value->region_id : 0;
                    $citiesName = $value->region_name;
                }
                $message .= "<option value='{$value->region_id}'>{$value->region_name}</option>";
            }

            /**
             * 第一个城市的所有地区
             */
            if ( 0 < $citiesId  && 2 == $type ) {
                $districtResult = $this->region->findByType($citiesId, $type+1);
                foreach($districtResult->result() as $key=>$value) {
                    if ( $key == 0 ) {
                        $districtName = $value->region_name;
                    }
                    $district .= "<option value='{$value->region_id}'>{$value->region_name}</option>";
                }
            }
        }
        echo json_encode(
            array(
                'status'   => $status,
                'message'  => $message,
                'district' => $district,
                'citiesName' => $citiesName,
                'districtName' => $type == 2 ? $districtName : $citiesName
            )
        );
        exit;
    }

    /**
     * 游币专区
     */
    public function voucher()
    {
        $getData = array('sort'=> 1, 'bonus' => 1);
        $data['account'] = $this->user_account->findByUserAccount($this->uid)->row();
        $data['scenicList'] = $this->scenery_base->search($getData, 4, 0);
        $data['tourismList'] = $this->tourism_goods_base->findBzCurrencyTourism(array('swim' => 1, 'limit' => 4));
        $this->load->view('home/winners/voucher', $data);
    }

    /**
     * 游币说明
     */
    public function bonus()
    {
        $this->load->view('home/winners/bonus');
    }
}
